angular.module('mvcRestBaseApp')
    .factory('Product', ['$http',
        function ($http) {
            var model = {
                listProducts: function (shopId, page, pageSize) {
                    shopId = shopId || '';
                    page = page || 1;
                    pageSize = pageSize || 10;
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/product/list?shop=' + shopId + '&page=' + page + '&page_size=' + pageSize)
                            .success(function (data) {
                                if (data.code === 0) {
                                    resolve(data.data.records, data.data.total);
                                } else {
                                    reject(data.data);
                                }
                            }).error(reject);
                    });
                },
                listProductComments: function(productId, page, pageSize) {
                    page = page || 1;
                    pageSize = pageSize || 10;
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/product/comments/'+productId + '?page=' + page + '&page_size=' + pageSize)
                            .success(function (data) {
                                if (data.code === 0) {
                                    resolve({records: data.data.records, total: data.data.total});
                                } else {
                                    reject(data.data);
                                }
                            }).error(reject);
                    });
                },
                listProductChanges: function (page, pageSize) {
                    page = page || 1;
                    pageSize = pageSize || 10;
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/product/list_changes?page=' + page + '&page_size=' + pageSize)
                            .success(function (data) {
                                if (data.code === 0) {
                                    resolve(data.data.records, data.data.total);
                                } else {
                                    reject(data.data);
                                }
                            }).error(reject);
                    });
                },
                searchProducts: function (keyword, page, pageSize) {
                    keyword = keyword || '';
                    page = page || 1;
                    pageSize = pageSize || 10;
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/product/search',
                            method: 'POST',
                            headers: formHeaders,
                            data: $.param({
                                keyword: keyword,
                                page: page,
                                page_size: pageSize
                            })
                        }).success(function (data) {
                            if (data.code === 0) {
                                resolve({records: data.data.records, total: data.data.total});
                            } else {
                                reject(data.data);
                            }
                        }).error(reject);
                    });
                },
                deleteComments: function(ids) {
                    return new Promise(function(resolve, reject) {
                        $http({
                            url: '/wechatshop/product/delete_comments',
                            method: 'POST',
                            headers: formHeaders,
                            data: $.param({
                                ids: ids.join(',')
                            })
                        }).success(resolve).error(reject);
                    });
                },
                createProduct: function (name, description, price, discount, shopId, logoUrl) {
                    discount = discount || 0;
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/product/create',
                            method: 'POST',
                            headers: formHeaders,
                            data: $.param({
                                name: name,
                                description: description,
                                logoUrl: logoUrl,
                                shopId: shopId,
                                price: price,
                                discount: discount
                            })
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                updateProduct: function (productId, changedProperties) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/product/update/' + productId,
                            method: 'POST',
                            headers: formHeaders,
                            data: $.param(changedProperties)
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                getProduct: function (id) {
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/product/info/' + id)
                            .success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                getLatestProductComments: function (productId) {
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/product/comments/' + productId)
                            .success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                addProductComment: function (productId, title, content) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/product/add_comment',
                            method: 'POST',
                            headers: formHeaders,
                            data: $.param({
                                productId: productId,
                                title: title,
                                content: content
                            })
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                collectProduct: function (productId) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/product/collect/' + productId,
                            method: 'POST',
                            headers: formHeaders
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                unCollectProduct: function (productId) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/product/uncollect/' + productId,
                            method: 'POST',
                            headers: formHeaders
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                hasCollectedProduct: function (productId) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/product/has_collected/' + productId,
                            method: 'POST',
                            headers: formHeaders
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                getUserCollectedProducts: function (userId, page, pageSize) {
                    page = page || 1;
                    pageSize = pageSize || 10;
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/product/user_collected/' + userId + '?page=' + page + '&page_size=' + pageSize)
                            .success(function (data) {
                                if (data.code === 0) {
                                    resolve(data.data.records, data.data.total);
                                } else {
                                    reject(data.data);
                                }
                            })
                            .error(reject);
                    });
                },
                getHotestProducts: function (limit) {
                    limit = limit || 5;
                    return new Promise(function (resolve, reject) {
                        $http.get('/wechatshop/product/hotest?limit=' + limit)
                            .success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                agreeProductChange: function (id) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/product/agree_product_change/' + id,
                            method: 'POST'
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                },
                disagreeProductChange: function (id) {
                    return new Promise(function (resolve, reject) {
                        $http({
                            url: '/wechatshop/product/disagree_product_change/' + id,
                            method: 'POST'
                        }).success(ngPromiseHandler(resolve, reject))
                            .error(reject);
                    });
                }
            };
            return model;
        }]);